package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.system.domain.IndexManagement;
import org.dromara.system.domain.IndexManagementDetail;
import org.dromara.system.domain.bo.IndexManagementBo;
import org.dromara.system.domain.vo.IndexManagementDetailVo;
import org.dromara.system.domain.vo.IndexManagementVo;
import org.dromara.system.mapper.IndexManagementDetailMapper;
import org.dromara.system.mapper.IndexManagementMapper;
import org.dromara.system.service.IIndexManagementService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 指标管理Service业务层处理
 *
 * @author Lion Li
 * @date 2025-10-23
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class IndexManagementServiceImpl implements IIndexManagementService {

    private final IndexManagementMapper baseMapper;

    private final IndexManagementDetailMapper detailMapper;

    /**
     * 查询指标管理列表
     *
     * @return 指标管理列表
     */
    @Override
    public List<IndexManagementVo> queryList() {
        LambdaQueryWrapper<IndexManagement> lqw = new LambdaQueryWrapper<>();
        List<IndexManagementVo> result = baseMapper.selectVoList(lqw);
        if (result.isEmpty()) {
            return result;
        }
        List<Long> managementIds = result.stream().map(IndexManagementVo::getId).collect(Collectors.toList());
        LambdaQueryWrapper<IndexManagementDetail> detailLqw = new LambdaQueryWrapper<>();
        detailLqw.in(IndexManagementDetail::getManagementId, managementIds);
        List<IndexManagementDetailVo> detailList = detailMapper.selectVoList(detailLqw);
        Map<Long, List<IndexManagementDetailVo>> detailMap = detailList.stream().collect(Collectors.groupingBy(IndexManagementDetailVo::getManagementId));
        result.forEach(indexManagement ->
            indexManagement.setIndexManagementDetailList(
                detailMap.getOrDefault(indexManagement.getId(), Collections.emptyList())
            ));
        return result;
    }

    /**
     * 批量新增指标管理
     *
     * @param indexManagementBoList 指标管理列表
     * @return 是否新增成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchInsert(List<IndexManagementBo> indexManagementBoList) {
        if (CollectionUtils.isEmpty(indexManagementBoList)) {
            throw new ServiceException("指标管理参数不能为空");
        }
        indexManagementBoList.forEach(indexManagementBo -> {
            IndexManagement convert = MapstructUtils.convert(indexManagementBo, IndexManagement.class);
            baseMapper.insert(convert);
            List<IndexManagementDetail> indexManagementDetailList = MapstructUtils.convert(indexManagementBo.getIndexManagementDetailList(), IndexManagementDetail.class);
            if (!CollectionUtils.isEmpty(indexManagementDetailList) && convert != null) {
                indexManagementDetailList.forEach(detail -> detail.setManagementId(convert.getId()));
                detailMapper.insert(indexManagementDetailList);
            }
        });
        return true;
    }


    /**
     * 批量修改指标管理
     *
     * @param indexManagementBoList 指标管理列表
     * @return 是否修改成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchUpdate(List<IndexManagementBo> indexManagementBoList) {
        if (CollectionUtils.isEmpty(indexManagementBoList)) {
            throw new ServiceException("指标管理参数不能为空");
        }
        List<IndexManagement> indexManagementList = MapstructUtils.convert(indexManagementBoList, IndexManagement.class);
        return baseMapper.updateBatchById(indexManagementList);
    }
}
